﻿//Chart module. Text Report and graph.
//This javascript file initializes pop-up window
//and creates Text Report table with currency dynamic.

//Object that contains information about curent rate for pop-up window
function RateDynamic() {
	this.date;
	this.value;
}

// Graphical drawing functions incapsulated in following class
function Chart (dataSource, WIDTH, HEIGHT) {
	// a variable storing the current state of graph
	var data = ""; //@private
	// a variable storing the chart input
	this.dataSource = dataSource;
	this.minValue = 0;
	this.maxValue = 0;
	this.WIDTH = WIDTH;
	this.HEIGHT = HEIGHT;
	this.CHART_OFFSET = 3; // Chart padding in pixels
	this.CELL_SIZE = 75; // Size of grid cell in pixels
	
	this.graph = new StructuredGraphicControl (0, 0, this.WIDTH, this.HEIGHT);

	//Function that draw graph
	this.drawGraph = function ()
	{
		var dataSourceLength = this.dataSource.length;

		//Maximum and minimum values of graph
		var maxValue = parseFloat(dataSource[0].value);
		var minValue = parseFloat(dataSource[0].value);
		var tmp = 0;
		for (var i = 1; i < dataSourceLength; i++)
		{
			tmp = parseFloat(this.dataSource[i].value);
			
			if (minValue > tmp) minValue = tmp;
			if (maxValue < tmp) maxValue = tmp;
		}
		
		var delta = maxValue - minValue;

		this.minValue = minValue;
		this.maxValue = maxValue;

		//Array of reformed values
		var coordinates = new Array();

		for (var i = 0; i < dataSourceLength; i++)
		{
			coordinates [i] =
					this.HEIGHT -     this.CHART_OFFSET  -
				((this.HEIGHT - 2 * this.CHART_OFFSET) *
				 (parseFloat(this.dataSource[i].value) - minValue)) / delta;
		}
		
		this.graph.setColor(255,0,0);
		var deltaX = (dataSourceLength ? (this.WIDTH - 2*this.CHART_OFFSET - 1) / (dataSourceLength - 1) : 0);
		var currentX = this.CHART_OFFSET + deltaX;
		this.graph.moveTo(this.CHART_OFFSET, coordinates [0]);

		for (var i = 1; i < dataSourceLength; i++)
		{
			this.graph.lineTo(currentX, coordinates [i]);
			currentX += deltaX;
		}

		data += this.graph.getHtmlString();
	};
	//Function that draw values on axes
	this.drawValues = function()
	{
		var indexOfDot = dataSource[0].value.indexOf(".");
		var zeroQuan = indexOfDot>0?this.dataSource[0].value.substr(indexOfDot + 1).length:0;
		
		var reformedValue = normalizeFloat(this.maxValue, zeroQuan);
		
		var values = "";
		// Draw Y values
		for (var i=0; i<=this.HEIGHT; i+=this.CELL_SIZE)
		{
			var reformedValue = normalizeFloat(this.maxValue - i * (this.maxValue - this.minValue) / this.HEIGHT , zeroQuan);
			values += this.getText(-8*reformedValue.length, i-8, formatValueToRegStd(reformedValue));
		}
		// Draw X values
		for (var i=0; i<=this.WIDTH; i+=this.CELL_SIZE)
		{
			values += this.getText(i - 16, this.HEIGHT + 16, this.dataSource[parseInt(i*(dataSource.length-1)/this.WIDTH)].date.substr(0, 5));
		}
		data += values;		
	};
	
	// Function wich adds a div-block with given text at current position to chart
	this.getText = function(x, y, text) {
		return ("<div class='chartLabel' style='left:" + x + "px;top:" + y + "px;'>" + text + "</div>");
	};
	
	this.draw = function()
	{
		this.drawGraph();
		this.drawValues();
		return "<div class='chart' style='" + "height:" + this.HEIGHT + "px; width:" + this.HEIGHT + "px'>" + data + "</div>";		
	};
}

//Function that create table body
function getReportTableBody(dataSource)
{
	var length = dataSource.length;
	var today = new Date();
	// the flag that marks that rates for tomorrow is available
	var tomorrowIsAvailable = (today < parseDateFromString(dataSource[length-1].date));
	var period = 12;  // the period of data's representing


	var indexOfDot = dataSource[0].value.indexOf(".");
	var zeroQuan = indexOfDot>0?dataSource[0].value.substr(indexOfDot + 1).length:0;
	
	var tbody = "";
	for (var i = length-1; i > length-period; i--) {
		if (tomorrowIsAvailable) {
			tbody += "<tr class='tomorrow'><td>" + prefs.getMsg("tomorrow") + "</td>";
			tomorrowIsAvailable = false;
		}
		else {
			tbody += "<tr><td></td>";
		}
		tbody += "<td align='center'>" + dataSource[i].date + "</td>";
		tbody += "<td align='right'>" + formatValueToRegStd(dataSource[i].value) + "</td>";
		var difference = parseFloat(dataSource[i].value) - parseFloat(dataSource[i-1].value);
		tbody += "<td align='right' " + ((difference > 0) ? "class='increase'>+" : "class='decrease'>") + formatValueToRegStd(normalizeFloat(difference, zeroQuan)) + "</td></tr>";
	}

	return tbody;
}
//Function that create report table
function createReportTable(dataSource)
{
	return "<table id='report' cellspacing='0' align='center'>"+getReportTableBody(dataSource)+"</table>";
}

//Function that returns popup page body
function createBody(dataSource, title)
{
	//Initial width and height of the area where graph will be constructed.
	var CHART_WIDTH  = 300;
	var CHART_HEIGHT = 300;

	var head = 	"<head>"+
								"<title>" + prefs.getMsg("title") + " : " + prefs.getMsg("quotations")+"</title>" +
								"<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>" +
								"<link rel='stylesheet' type='text/css' href='http://finance-google-gadget.googlecode.com/svn/trunk/src/chart.css'>" +
							"</head>";

	var printButton = '<a onclick="window.print(); return false;" id="printButton" href="#">'+prefs.getMsg("print")+'</a>';

	var logo = "<a href='http://finance.gadget.googlepages.com' target='_blank'>" + 
								"<img id='logo' src='http://finance-google-gadget.googlecode.com/svn/trunk/src/logo.png' align='absbottom'" +
											" alt='" + prefs.getMsg("title") + "' title='" + prefs.getMsg("title") +"'/>" +
							"</a>";

	var chart = new Chart(dataSource, CHART_WIDTH, CHART_HEIGHT);
	
	var reportTable = createReportTable(dataSource);

	var mainTitle = "<h1>" + logo + title + "</h1>";

	var mainTable = 
		"<table id ='mainTable' cellpadding='10'>" +
			"<thead>" +
				"<tr><td>" + prefs.getMsg("for_three_month") + "</td><td>" + prefs.getMsg("for_two_weeks") + "</td></tr>" +
			"</thead>" +
			"<tbody>" +
				"<tr><td>" + chart.draw() + "</td><td>" + reportTable + "</td></tr>" +
			"</tbody>" +
		"</table>";
	
	return head + "<body>" + mainTitle + mainTable + "</body>";
}

//Function that initialize pop-up window and pass to it string with report table
function CreateReportWindow(dataSource, title)
{
	_IG_Analytics("UA-1739928-1", "/finance-google-gadget/chart");
	
	//Create html
	var html = '<html>' + createBody(dataSource, title) + '</html>';

	//create new sub window
	var chartWindow = window.open("about:blank" , "chartWindow", "height=520,width=730,status=no,menubar=no,resizable=yes,scrollbars=0,toolbar=0,location=0,directories=0");

	//fill new sub window
	chartWindow.document.write(html);
	chartWindow.document.close();
	chartWindow.focus();
}